#ifndef _XML_H_
#define _XML_H_

#include <string>
#include <vector>

#include <system/arquivo.h>

class XML;
class XMLElement;
class XMLAttribute;



// XML ----------------------------------------------------------

class XML : public Arquivo
{

public:

    XML();
    virtual ~XML();

    inline void Version(std::string version) {m_version = version;}
    inline void Encoding(std::string encoding) {m_encoding = encoding;}
    inline void RootName(std::string rootName) {m_rootName = rootName;}

    bool Abrir();
    bool Abrir(std::string strNomeDoArquivo);
    XML& LoadXML(std::string codigo);
    std::string str();
    const char* c_str();

    XMLElement& AddElement(std::string tag);
    XML&        AddAttribute(std::string id, std::string valor);
    XMLElement& operator[](int n);

private:

    std::string   m_version;
    std::string   m_encoding;
    std::string   m_rootName;
    XMLElement*   m_firstElement;
    XMLAttribute* m_firstAttribute; // Atributo da raiz
    std::string   m_code;

    std::string PegarCodigoInterno(XMLElement* elem, int nIdentacao);

};


// XMLElement ----------------------------------------------------------

class XMLElement
{

public:

    XMLElement(std::string tag);
    virtual ~XMLElement();

    XMLElement*   Next() const {return m_nextElement;}
    XMLElement*   FirstChild() const {return m_firstChild;}
    XMLAttribute* FirstAttribute() const {return m_firstAttribute;}
    std::string   Tag() const {return m_tag;}
    std::string   Text() const {return m_text;}

    inline void Next(XMLElement* next) {m_nextElement = next;}

    XMLElement& AddElement(std::string tag);
    XMLElement& AddAttribute(std::string id, std::string valor);
    XMLElement& AddText(std::string text);

    XMLElement&         operator[](int n);
    XMLAttribute&       operator[](std::string attribute);
    bool                HasAttribute(std::string id);

private:

    XMLElement*   m_nextElement;
    XMLElement*   m_firstChild;
    XMLAttribute* m_firstAttribute;
    std::string   m_tag;
    std::string   m_text;

};


// XMLAttribute ----------------------------------------------------------

class XMLAttribute
{

public:

    XMLAttribute(std::string id, std::string valor);
    virtual ~XMLAttribute();

    std::string   Id() const {return m_id;}
    std::string   Valor() const {return m_valor;}
    XMLAttribute* Next() const {return m_nextAttribute;}

    inline void Id(std::string id) {m_id = id;}
    inline void Valor(std::string valor) {m_valor = valor;}
    inline void Next(XMLAttribute* nextAttribute) {m_nextAttribute = nextAttribute;}
    inline void operator=(std::string valor) {m_valor = valor;}

private:

    XMLAttribute* m_nextAttribute;
    std::string m_id;
    std::string m_valor;

};


#endif  // _XML_H_
